package com.zsc.wxm.ysh.anjo.es.crawler;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zsc.wxm.ysh.anjo.es.utils.FileUtils;
import com.zsc.wxm.ysh.anjo.service.base.constant.OtherConstant;
import com.zsc.wxm.ysh.anjo.utils.ExceptionUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * 描述：
 *
 * @author playmaker
 * @since 2021/2/13 22:11
 */
@Slf4j
public class DataPipeLine implements Pipeline {

    private static final ObjectMapper MAPPER = new ObjectMapper();

    @Override
    public void process(ResultItems resultItems, Task task) {
        Map<String, Object> data = new HashMap<>();
        data.put("url", resultItems.getRequest().getUrl());

        String title = resultItems.get("title");
        //标题
        data.put("title", title);

        //楼盘名称
        String[] s = StringUtils.split(title, ' ');
        String estateName = StringUtils.split(s[0],'·')[1];
        data.put("estateName", estateName);

        //租金
        data.put("rent", resultItems.get("rent"));
        String[] types = StringUtils.split(resultItems.get("type"), ' ');

        //租赁方式
        data.put("rentMethod", types[0]);
        //户型 如2室1厅1卫
        data.put("houseType", types[1]);
        //面积
        data.put("useArea", types[2]);
        //朝向
        data.put("orientation", types[3]);

        //描述
        if (resultItems.get("houseDesc") != null) {
            String[] descArray = StringUtils.split(resultItems.get("houseDesc"), ' ');
            StringBuilder desc = new StringBuilder();
            desc.append(descArray[1]);
            if (descArray.length > 3) {
                desc.append(descArray[3]);
            }
            data.put("houseDesc", desc.toString());
        } else {
            data.put("houseDesc", " ");
        }

        //房源编号
        String[] codes = StringUtils.split(resultItems.get("code"),'：');
        data.put("code", codes[1]);


        String[] infos = StringUtils.split(resultItems.get("info"), ' ');
        for (String info : infos) {
            if (StringUtils.startsWith(info, "看房：")) {
                data.put("time", StringUtils.split(info, '：')[1]);
            } else if (StringUtils.startsWith(info, "楼层：")) {
                data.put("floor", StringUtils.split(info, '：')[1]);
            }
        }
        //以下图片处理
        String imageUrl = StringUtils.split(resultItems.get("img"), '"')[3];
        String newName = StringUtils
                .substringBefore(StringUtils
                        .substringAfterLast(resultItems.getRequest().getUrl(),
                                "/"), ".") + ".jpg";
        try {
            FileUtils.downloadFile(imageUrl, new File(OtherConstant.IMAGE_SAVE_PATH + newName));
            data.put("image", newName);
            String json = MAPPER.writeValueAsString(data);
            org.apache.commons.io.FileUtils.write(new File(OtherConstant.JSON_DATA_PATH + "data.json"),
                    json + "\n", "UTF-8", true);
        } catch (Exception e) {
            log.error(ExceptionUtil.getMessage(e));
        }
    }
}
